<?php
namespace App\Http\Controllers\System;
use App\Http\Controllers\Controller;
use Symfony\Component\HttpFoundation\Request;
use DB;
class VersionController extends Controller{

    private $iosVersion = 107;
    //安卓正式版本
    public function current_version(){
        $info = DB::table('app_version')
            ->whereNull('deleted_at')
            ->where('is_show',1)
            ->where('client','Android')
            ->orderBy('id','desc')
            ->first();

        $content    = $info->content;
        $content    = str_replace(array("\r\n", "\r", "\n"), "", $content);
        $content    = explode(';',$content);
        $contents   = [];
        foreach($content as $p)
        {
            array_push($contents,['content'=>$p]);
        }

        $data = [
            "versionCode"   => $info->version_code,
            "versionName"   => $info->version_name,
            "mustUpgrade"   => 0,
            "updateUrl"     => env('HOST')."/".$info->apk_url,
            "updateList"    => $contents
        ];

        return response()->json($data);
    }


    //安卓测试版本
    public function test_version(){
        $data = [
            "versionCode"=>20170803,
            "versionName"=>"1.1.2",
            "updateUrl"=>"http://wx.laohoulundao.com/apk/Reading4.apk",
            "updateList"=>[
                ["content"=>"读书会可以互相评论和回复"],
                ["content"=>"老侯代读语音可以记录播放位置"],
                ["content"=>"读书改进计划自动保存为草稿"],
                ["content"=>"个人和企业读书会改版"],
                ["content"=>"企业读书会新增学习官服务"],
                ["content"=>"修复了一些已知的问题"]
            ]
        ];
        return response()->json($data);
    }


    //IOS版本检测  用于升级提示
    public function ios_version(Request $request){

        $preVersion = $request->input('version');
        $info = DB::table('app_version')
            ->whereNull('deleted_at')
            ->where('is_show',1)
            ->where('client','IOS')
            ->orderBy('id','desc')
            ->first();

        $newVersion = $info->version_code;
        if($preVersion < $newVersion){
            $upgrade = 1;
        }else{
            $upgrade = 0;
        }

        $data = [
            "upgrade" => $upgrade,
            "link"    => "http://test.wx.laohoulundao.com/app/version/iso_upgrade",
            "appUrl"  => "https://itunes.apple.com/cn/app/id1187561609?mt=8",
            "mustUpgrade"=>0
        ];
        return response()->json($data);
    }


    //IOS正式环境检测  用于动态域名
    public function Vironment(Request $request){
        $data   = [
            'domain'=>"http://wx.laohoulundao.com",
            "cache" =>1
        ];
        return json_encode($data);
    }

    //ios测试环境检测
    public function test_vironment(Request $request)
    {
       return $this->Vironment($request);
    }


    public function dev_vironment(Request $request)
    {
        $key    = $request->input('key');
        if($key == '')
        {
        }
        $data   = [
            'domain'=>"http://test.wx.laohoulundao.com",
            "cache" =>0
        ];
        return json_encode($data);
    }


    /**
     * 用于我的本地调试
     * */
    public function zhangweixi_vironment(){
        $data   = [
            //'domain'=>"http://test.wx.laohoulundao.com",
            'domain'=>"http://wx.laohoulundao.com",
            //'domain'=>"http://192.168.1.100",
            "cache" =>0
        ];

        return json_encode($data);
    }


}